package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.LegacySales;

public class LegacySalesPresenter extends AbstractPresenter<LegacySales> {
	final String SINGLEITEM = "legacySales";
	final String MULTIITEM = "legacySaless";
	
	public ArrayList<LegacySales> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public LegacySales tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public LegacySales createItemFromJSONObject(JSONObject jsonObject){
		LegacySales item = new LegacySales();
		try{
			item.setId(jsonObject.getInt("lsid"));
			item.setNumHalfBarrels(jsonObject.getInt("numHalf"));
			item.setNumQuarterBarrels(jsonObject.getInt("numQtr"));
			item.setNumSixtels(jsonObject.getInt("numSix"));
			item.setNumTwentyTwos(jsonObject.getInt("numTT"));
			
			POSBeerViewPresenter presenter = new POSBeerViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("beerID");
			item.setBeerId(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(LegacySales item){
		try{
			JSONObject json = new JSONObject();
			json.put("lsid", item.getId());
			json.put("numHalf", item.getNumHalfBarrels());
			json.put("numQtr", item.getNumQuarterBarrels());
			json.put("numSix", item.getNumSixtels());
			json.put("numTT", item.getNumTwentyTwos());

			POSBeerViewPresenter presenter = new POSBeerViewPresenter();
			json.put("beerID", presenter.createJSON(item.getBeerId()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public LegacySales getEmptyEntity(){
		LegacySales entity = new LegacySales();
		return entity;
	}
}
